package io.zhengsh.vvip.leetcode.binarysearch;

/**
 * 704. 二分查找
 */
public class Q704_Binary_Search {
    class Solution {
        public int search(int[] nums, int target) {
            int left = 0;
            int right = nums.length - 1;
            int index = -1;
            while (left <= right) {
                int mid = ((right - left) / 2) + left;
                if (nums[mid] == target) {
                    index = mid;
                    break;
                } else if (nums[mid] > target) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            return index;
        }
    }

}
